<?php

namespace app\admin\controller;

use think\Db;
use think\Request;

class Tg extends Base
{
    /**
     * 学校码展示
     * @return mixed|\think\response\Json
     */
    public function schcode(){
        if(request()->isAjax ()){
            $params = input('param.');
            $where = [];
            
            // 活动名称
            if(isset($params['school']) && $params['school'] != ''){
                $where['school'] = ['like', '%' . $params['school'] . '%'];
            }
            if(isset($params['type']) && $params['type'] != ''){
                $where['usetype'] = $params['type'];
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('offline_tg')->where($where)->count();// 获取总条数
            $lists = Db::name('offline_tg')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('time desc')
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 新建
     * @return mixed|\think\response\Json
     */
    public function activeAdd(){
        if(Request::instance()->isPost()){
            $school_id = input('school_id');
            $active_name = Db::table('up_school')->where('school_id',$school_id)->value('school_name');
            $type = input('type');
            $year = input('year');
            $startdate = input('startdate');
            $enddate = input('enddate');
            $grade = input('grade');
            //
            $have = Db::table('up_offline_tg')
                ->where('school_id',$school_id)
                ->where('year',$year)
                ->where('status',1)
                ->find();
            if($have){
                return json(['code' => 100, 'msg' => '此学校在当前年份已经有统购设置了']);
            }
            //
            $insert = [
                'school_id' => $school_id,
                'school' => $active_name,
                'year' => $year,
                'grade' => $grade,
                'usetype' => $type,
                'startdate' => $startdate,
                'enddate' => $enddate,
                'time' => date('Y-m-d H:i:s')
            ];
            if($type >= 2){
                $active_member_num = round(input('active_member_num'),2);
                $insert['money'] = $active_member_num;
            }
            $res = Db::table('up_offline_tg')->insertGetId($insert);
            if($res){
                return json(['code' => 200, 'msg' => '新建成功']);
            }else{
                return json(['code' => 100, 'msg' => '新建失败']);
            }
        }
        $schools = Db::table('up_school')->where('school_status',1)
            ->field('school_id,school_name,school_erp')->select();
        $this->assign('schools',$schools);
        return $this->fetch('tg/activeadd');
    }

    /**
     * 编辑
     * @return mixed|\think\response\Json
     */
    public function activeEdit(){
        $active_id = input('id');
        if(Request::instance()->isPost()){
            $type = input('type');
            $startdate = input('startdate');
            $enddate = input('enddate');
            $grade = input('grade');
            $update = [
                'grade' => $grade,
                'startdate' => $startdate,
                'enddate' => $enddate,
                'time' => date('Y-m-d H:i:s')
            ];
            if($type >= 2){
                $active_member_num = round(input('active_member_num'),2);
                $update['money'] = $active_member_num;
            }
            $res = Db::table('up_offline_tg')->where('id', $active_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        
        $info = Db::table('up_offline_tg')
            ->where('id', $active_id)
            ->find();
        $schools = Db::table('up_school')->where('school_status',1)
            ->field('school_id,school_name,school_erp')->select();
        $this->assign([
            'id' => $active_id,
            'info' => $info,
            'schools' => $schools,
        ]);
        return $this->fetch('tg/activeedit');
    }
    /**
     * 改状态（是否开启）
     * @author
     */
    public function changeStatus()
    {
        extract(input());
        $res = Db::table('up_offline_tg')
            ->where('id',$id)
            ->update([
                'time' => date('Y-m-d H:i:s'),
                'status' => $num
            ]);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
    /**
     * 改状态（是否开启）
     * @author
     */
    public function changeShouhouStatus()
    {
        extract(input());
        $res = Db::table('up_offline_tg')
            ->where('id',$id)
            ->update([
                'time' => date('Y-m-d H:i:s'),
                'shouhou' => $num
            ]);
        if($res){
            Db::table('up_offline_tgorder')
                ->where('tg_id',$id)
                ->setField('sale_can',2);
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function activeDel(){
        $active_id = input('id');
        $res = Db::table('up_offline_tg')->where('id', $active_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }

    /**
     * 订单
     * @return mixed|\think\response\Json
     */
    public function order(){
        if(request()->isAjax ()){
            $params = input('param.');
            $where = [];

            // 活动名称
            if(isset($params['school']) && $params['school'] != ''){
                $where['school|orderno|name|idcard|backno'] = ['like', '%' . $params['school'] . '%'];
            }
            // 状态
            if(isset($params['o_status']) && $params['o_status'] != ''){
                $where['status'] = $params['o_status'];
            }
            if(isset($params['type']) && $params['type'] != ''){
                $where['fromtype'] = $params['type'];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('offline_tgorder')->where($where)->count();// 获取总条数
            $lists = Db::name('offline_tgorder')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('time desc')
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function orderdel(){
        $active_id = input('id');
        $res = Db::table('up_offline_tgorder')->where('id', $active_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 审核
     * @return mixed|\think\response\Json
     */
    public function shenhe(){
        if(request()->isAjax ()){
            $params = input('param.');
            $where = [];
            $where['yes'] = 1;
            // 活动名称
            if(isset($params['school']) && $params['school'] != ''){
                $where['school'] = ['like', '%' . $params['school'] . '%'];
            }
            if(isset($params['type']) && $params['type'] != ''){
                $where['usetype'] = $params['type'];
            }

            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('offline_tg')->where($where)->count();// 获取总条数
            $lists = Db::name('offline_tg')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    /**
     * 同意
     * @return \think\response\Json
     */
    public function yesyes(){
        $active_id = input('id');
        $res = Db::table('up_offline_tg')->where('id', $active_id)->setField('yes',2);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败，请重试']);
        }
    }
    /**
     * 拒绝
     * @return \think\response\Json
     */
    public function nono(){
        $active_id = input('id');
        $res = Db::table('up_offline_tg')->where('id', $active_id)->setField('yes',3);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败，请重试']);
        }
    }
    /**
     * 导出Excel
     * @return \think\response\Json
     */
    public function excelwater(){
        set_time_limit(0);
        $params = input('param.');
        $where = [];
        // 活动名称
        if(isset($params['school']) && $params['school'] != ''){
            $where['school|orderno|name|idcard|backno'] = ['like', '%' . $params['school'] . '%'];
        }
        // 状态
        if(isset($params['o_status']) && $params['o_status'] != ''){
            $where['status'] = $params['o_status'];
        }
        if(isset($params['type']) && $params['type'] != ''){
            $where['fromtype'] = $params['type'];
        }
        $field = [
            'orderno','backno','school','grade','class','name','sex','idcard',
            'height','weight','size','sw_j','sw_x',
            'sw_y','sw_t','sw_b','sw_k',
            'phone', 'fromtype','type','status','money',
            'time'
        ];
        $data = Db::name('offline_tgorder')
            ->where($where)
            ->field($field)
            ->order('time desc')
            ->select();
        if($data){
            $cellname = [
                '订单号', '支付流水号', '学校', '年级', '班级', '学生姓名', '学生性别', '入学年份',
                '身高', '体重', '衣服尺码', '三围/肩宽', '三围/胸围',
                '三围/腰围', '三围/臀围', '三围/臂长', '三围/裤长',
                '注册手机', '记录类型', '支付类型', '订单状态', '支付金额',
                '支付时间'
            ];
            $status = ['status','fromtype','type'];
            $values = [
                'status' => ['1' => '未支付','2' => '已支付'],
                'fromtype' => ['1' => '仅收集','2' => '仅支付','3' => '收集兼支付'],
                'type' => ['1' => '微信','2' => '支付宝']
            ];
            $res = exportExcel('payonline',time(),$cellname,$data,$status,$values);
            return json($res);
        }else{
            return json(['code'=>0,'msg'=>'此条件下无数据']);
        }
    }
}